<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
const router = useRouter()
let registUser = reactive({
  username: '',
  password: '',
  confirmUserPwd: ''
})

let usernameMsg = ref('')
let userPwdMsg = ref('')
let confirmUserPwdMsg = ref('')

//用户名格式验证
function chickUsername() {
  //定义正则
  const usernameReg = /^[a-zA-Z0-9]{5,16}$/
  if (!usernameReg.test(registUser.username)) {
    usernameMsg.value = '用户名格式不正确，请输入5-16位字母或数字'
    return false
  }
  usernameMsg.value = 'ok'
  return true
}
//密码格式验证
function chickUserPwd() {
  //定义正则
  const userPwdReg = /^[a-zA-Z0-9]{6,20}$/
  if (!userPwdReg.test(registUser.password)) {
    userPwdMsg.value = '密码格式不正确，请输入6-20位字母或数字'
    return false
  }
  userPwdMsg.value = 'ok'
  return true
}
//confirm密码格式验证
function chickConfirmUserPwd() {
  if (registUser.confirmUserPwd !== registUser.password) {
    confirmUserPwdMsg.value = '两次密码输入不一致'
    return false
  }
    confirmUserPwdMsg.value = 'ok'
  return true
}

function regist() {
  if (chickUsername() && chickUserPwd() && chickConfirmUserPwd()) {
    alert('注册成功,清前往登录')
    router.push('/login')
  }
}

function reset() {
  registUser.username = ''
  registUser.password = ''
  registUser.confirmUserPwd = ''
  usernameMsg.value = ''
  userPwdMsg.value = ''
  confirmUserPwdMsg.value = ''
}
</script>

<template>
  <!-- 外层容器：居中显示 -->
  <div class="register-wrapper">
    <!-- 注册卡片：带阴影和圆角 -->
    <div class="register-card">
      <h3 class="register-title">新用户注册</h3>
      
      <!-- 表单区域 -->
      <form class="register-form">
        <div class="form-item">
          <label class="form-label">账号</label>
          <input 
            type="text" 
            class="form-input"
            name="username" 
            v-model="registUser.username" 
            @blur="chickUsername()" 
            @input="chickUsername()"
            placeholder="请输入5-16位字母或数字"
          >
          <span class="form-message" :class="{error: usernameMsg !== 'ok', success: usernameMsg === 'ok'}">
            {{ usernameMsg }}
          </span>
        </div>

        <div class="form-item">
          <label class="form-label">密码</label>
          <input 
            type="password" 
            class="form-input"
            name="userPwd" 
            v-model="registUser.password" 
            @blur="chickUserPwd()" 
            @input="chickUserPwd()"
            placeholder="请输入6-20位字母或数字"
          >
          <span class="form-message" :class="{error: userPwdMsg !== 'ok', success: userPwdMsg === 'ok'}">
            {{ userPwdMsg }}
          </span>
        </div>

        <div class="form-item">
          <label class="form-label">确认密码</label>
          <input 
            type="password" 
            class="form-input"
            name="confirmPassword" 
            v-model="registUser.confirmUserPwd" 
            @blur="chickConfirmUserPwd()" 
            @input="chickConfirmUserPwd()"
            placeholder="请再次输入密码"
          >
          <span class="form-message" :class="{error: confirmUserPwdMsg !== 'ok', success: confirmUserPwdMsg === 'ok'}">
            {{ confirmUserPwdMsg }}
          </span>
        </div>

        <!-- 按钮和登录链接区域 -->
        <div class="form-actions">
          <button type="button" class="btn register-btn" @click="regist()">注册</button>
          <button type="button" class="btn reset-btn" @click="reset()">重置</button>
          <router-link to="/login" class="login-link">
            已有账号？去登录
          </router-link>
        </div>
      </form>
    </div>
  </div>
</template>

<style scoped>
/* 外层容器：全屏居中 */
.register-wrapper {
  width: 100%;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f5f7fa;
  padding: 20px;
  box-sizing: border-box;
}

/* 注册卡片：白色背景+阴影+圆角 */
.register-card {
  width: 100%;
  max-width: 420px;
  background-color: #ffffff;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  padding: 36px;
  box-sizing: border-box;
}

/* 标题样式 */
.register-title {
  font-size: 22px;
  font-weight: 600;
  color: #1d2129;
  text-align: center;
  margin: 0 0 30px;
  letter-spacing: 0.5px;
}

/* 表单样式 */
.register-form {
  width: 100%;
}

/* 表单项容器 */
.form-item {
  margin-bottom: 24px;
  position: relative;
}

/* 标签样式 */
.form-label {
  display: block;
  font-size: 14px;
  color: #4e5969;
  margin-bottom: 8px;
  font-weight: 500;
}

/* 输入框样式 */
.form-input {
  width: 100%;
  height: 44px;
  padding: 0 14px;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  font-size: 14px;
  color: #1d2129;
  box-sizing: border-box;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

/* 输入框交互：聚焦时高亮 */
.form-input:focus {
  outline: none;
  border-color: #409eff;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.1);
}

/* 提示信息样式 */
.form-message {
  display: block;
  margin-top: 6px;
  font-size: 12px;
  height: 16px;
  transition: color 0.2s ease;
}

/* 错误提示颜色 */
.error {
  color: #f56c6c;
}

/* 成功提示颜色 */
.success {
  color: #67c23a;
}

/* 按钮和链接区域 */
.form-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 12px;
  flex-wrap: wrap;
  gap: 10px;
}

/* 按钮基础样式 */
.btn {
  padding: 0 20px;
  height: 44px;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  border: none;
}

/* 注册按钮样式 */
.register-btn {
  background-color: #409eff;
  color: #ffffff;
}

/* 注册按钮 hover 效果 */
.register-btn:hover {
  background-color: #3391e8;
  transform: translateY(-1px);
}

/* 重置按钮样式 */
.reset-btn {
  background-color: #ffffff;
  color: #4e5969;
  border: 1px solid #d1d5db;
}

/* 重置按钮 hover 效果 */
.reset-btn:hover {
  background-color: #f5f7fa;
  transform: translateY(-1px);
}

/* 登录链接样式 */
.login-link {
  font-size: 12px;
  color: #409eff;
  text-decoration: none;
  transition: color 0.2s ease;
  white-space: nowrap;
}

/* 登录链接 hover 效果 */
.login-link:hover {
  color: #3391e8;
  text-decoration: underline;
}

/* 适配小屏幕 */
@media (max-width: 420px) {
  .form-actions {
    justify-content: center;
  }
  
  .register-card {
    padding: 24px;
  }
}
</style>
